package org.jmat.batch.listeners;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.listener.StepListenerSupport;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.util.StopWatch;

public class StepExecutionListener extends StepListenerSupport {
	
	private final Log log = LogFactory.getLog(StepExecutionListener.class);
	private StopWatch watch = new StopWatch();

	 public void beforeStep(StepExecution stepExecution){
		 watch.start();
		 if (log.isDebugEnabled())
			 log.debug("Begin step : " + stepExecution.getStepName());
	 }

    public ExitStatus onErrorInStep(StepExecution stepExecution, Throwable e){
    	log.fatal("Error on " + stepExecution.getStepName());
		return ExitStatus.FAILED;
    	
    }

    public ExitStatus afterStep(StepExecution stepExecution){
    	watch.stop();
    	if (log.isDebugEnabled()){
    		log.debug("End step : " + stepExecution.getStepName() + " in " + watch.getTotalTimeSeconds() + " sec"); 
    	}
		return ExitStatus.FINISHED;
    	
    }

}
